package com.ruicar.afs.cloud.basic.common.controller;

import cn.hutool.core.date.DateUtil;
import com.ruicar.afs.cloud.basic.common.condition.WorkDaysRequestCondition;
import com.ruicar.afs.cloud.basic.common.condition.WorkDaysResponseCondition;
import com.ruicar.afs.cloud.batch.entity.AfsBatchInfo;
import com.ruicar.afs.cloud.batch.frame.control.BatchHelper;
import com.ruicar.afs.cloud.batch.service.AfsBatchInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 获取合同系统相关的日期的操作
 */
@Slf4j
@RestController
@RequestMapping("/workDays")
@AllArgsConstructor
public class WorkDaysController {

    private final AfsBatchInfoService afsBatchInfoService;

    @PostMapping(value = "/queryWorkDays")
    @ApiOperation(value = "查询工作日")
    public IResponse<WorkDaysResponseCondition> queryWorkDays(@RequestBody WorkDaysRequestCondition requestCondition){
        List<String> days = new ArrayList<String>();

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(DateUtil.parse(requestCondition.getStartDate()));
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(DateUtil.parse(requestCondition.getEndDate()));

        tempEnd.add(Calendar.DATE, +1);//日期加1(包含结束)

        while (tempStart.before(tempEnd)) {
            if(!BatchHelper.isHoliday(DateUtil.format(tempStart.getTime(),"yyyyMMdd"))){
                days.add(DateUtil.format(tempStart.getTime(),"yyyy-MM-dd"));
            }
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        WorkDaysResponseCondition responseCondition = new WorkDaysResponseCondition();
        responseCondition.setStartDate(requestCondition.getStartDate());
        responseCondition.setEndDate(requestCondition.getEndDate());
        responseCondition.setWorkdays(days);
        return new IResponse<WorkDaysResponseCondition>().setData(responseCondition);
    }

    @GetMapping(value = "/getBatchDate")
    @ApiOperation(value = "获取合同系统跑批日")
    public IResponse<String> getBatchDate() {
        AfsBatchInfo afsBatchInfo = afsBatchInfoService.getSystemBatchInfo("icos");
        String result = DateUtil.format(DateUtil.parse(afsBatchInfo.getBatchDate()), "yyyy-MM-dd");
        return new IResponse<String>().setData(result);
    }

}
